package com.yy.module.action;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.page.Message;
import com.yy.base.util.ObjectUtil;
import com.yy.base.util.StringUtil;
import com.yy.module.entity.Field;
import com.yy.module.entity.ModuleField;
import com.yy.module.entity.ModuleInfo;
import com.yy.module.entity.ModuleQueryField;
import com.yy.module.service.ModuleService;

@Controller
@RequestMapping(value = "/moduleAction")
@SuppressWarnings("unchecked")
public class ModuleAction extends BaseCtrl {
	private static final long serialVersionUID = 1L;
	@Autowired
	private ModuleService service;

	/**
	 * 查询模块展示信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleFieldList.html")
	public Message getModuleFieldList(String moduleId) {
		Message message = new Message();
		if (moduleId == null) {
			message.setPageList(new ArrayList());
			return message;
		}
		message.setPageList(service.getModuleFieldList(moduleId));
		return message;
	}
	/**
	 * 查询模块查询条件信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleQueryList.html")
	public Message getModuleQueryList(String moduleId) {
		Message message = new Message();
		if (moduleId == null) {
			message.setPageList(new ArrayList());
			return message;
		}
		message.setPageList(service.getModuleQueryList(moduleId));
		return message;
	}
	/**
	 * 查询所有模块信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleList.html")
	public Message getModuleList(HttpServletRequest request) {
		ModuleInfo module = ObjectUtil.mapToEntity(ModuleInfo.class, this.getParameterMap(request));
		Message message = new Message();
		if (module == null) {
			module = new ModuleInfo();
		}
		message.setPageList(service.getModuleList(module));
		return message;
	}
	/**
	 * 根据ID获取模块信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleInfoById.html")
	public Message getModuleInfoById(HttpServletRequest request) {
		Message message = new Message();
		message.formartData(service.getModuleInfoById(request.getParameter("id")));
		return message;
	}
	/**
	 * 根据ID获取模块信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleFieldById.html")
	public Message getModuleFieldById(HttpServletRequest request) {
		Message message = new Message();
		message.formartData(service.getModuleFieldById(request.getParameter("id")));
		return message;
	}
	/**
	 * 根据ID获取模块信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleQueryById.html")
	public Message getModuleQueryById(HttpServletRequest request) {
		Message message = new Message();
		message.formartData(service.getModuleQueryById(request.getParameter("id")));
		return message;
	}
	/**
	 * 批量保存字段
	 */
	@ResponseBody
	@RequestMapping(value="/batchSaveField.html")
	public Message batchSaveField(HttpServletRequest request) {
		ModuleField field = ObjectUtil.mapToEntity(ModuleField.class, this.getParameterMap(request));
		Message message = new Message();
		String []ids = request.getParameter("ids").split(",");
		String moduleId = request.getParameter("moduleId");
		String type = request.getParameter("type");
		Field f = null;
		ModuleField mf = null;
		field = new ModuleField();
		field.setModuleId(moduleId);
		
		for (int i = 0; i < ids.length; i++) {
			f = service.get(Field.class, ids[i]);
			mf = service.getModuleField(field.getModuleId(), f.getTableId(), f.getName());
			//判断是否重复添加字段
			if (mf != null) {
				//如果字段被删除就启用这个字段，不重复添加
				service.update(mf);
			} else {
				field.setName(f.getText());
				field.setTableId(f.getTableId());
				field.setColumnId(f.getId());
				field.setFieldName(f.getName());
				field.setIsNotNull(f.getIsNotNull());
				field.setType(Field.DATE.equals(f.getTypeId()) ? "date" : (f.getLength() > 100 ? "textarea" : "text"));
				field.setIsHide(f.getIsId() == null ? 0 : f.getIsId());
				if ("list".equals(type)) {
					field.setWidth(60);
				}
				field.setOrderBy((i + 1) * 10);
				field.setIsId(f.getIsId());
				
				service.save(field, "t_module_field");
			}
		}
		message.setOkMessage("添加成功！");
		return message;
	}
	/**
	 * 批量保存查询字段
	 */
	@ResponseBody
	@RequestMapping(value="/batchSaveQuery.html")
	public Message batchSaveQuery(HttpServletRequest request) {
		ModuleQueryField query = ObjectUtil.mapToEntity(ModuleQueryField.class, this.getParameterMap(request));
		Message message = new Message();
		String []ids = request.getParameter("ids").split(",");
		String moduleId = request.getParameter("moduleId");
		Field f = null;
		ModuleQueryField mf = null;
		query = new ModuleQueryField();
		query.setModuleId(moduleId);
		
		for (int i = 0; i < ids.length; i++) {
			f = service.get(Field.class, ids[i]);
			mf = service.getModuleQueryField(query.getModuleId(), f.getTableId(), f.getName());
			//判断是否重复添加字段
			if (mf != null) {
				//如果字段被删除就启用这个字段，不重复添加
				service.update(mf);
			} else {
				query.setName(f.getText());
				query.setTableId(f.getTableId());
				query.setFieldName(f.getName());
				
				query.setType(Field.DATE.equals(f.getTypeId()) ? "date" : (f.getLength() > 100 ? "textarea" : "text"));
				query.setIsHide(f.getIsId());
				query.setOrderBy(10);
				
				service.save(query, "t_module_query");
			}
		}
		message.setOkMessage("添加成功！");
		return message;
	}
	/**
	 * 保存页面内容
	 */
	@ResponseBody
	@RequestMapping(value="/saveField.html")
	public Message saveField(HttpServletRequest request) {
		Message message = new Message();
		ModuleField field = ObjectUtil.mapToEntity(ModuleField.class, this.getParameterMap(request));
		if (StringUtil.isNotEmpty(field.getId())) {
			service.update(field, "t_module_field");
			message.setOkMessage("保存成功！");
		} else {
			message.setErrorMessage("没找到传入ID！");
		}
		return message;
	}
	/**
	 * 保存页面查询条件
	 */
	@ResponseBody
	@RequestMapping(value="/saveQueryField.html")
	public Message saveQueryField(HttpServletRequest request) {
		ModuleQueryField query = ObjectUtil.mapToEntity(ModuleQueryField.class, this.getParameterMap(request));
		Message message = new Message();
		if (StringUtil.isNotEmpty(query.getId())) {
			service.update(query, "t_module_query");
			message.setOkMessage("保存成功！");
		} else {
			message.setErrorMessage("没找到传入ID！");
		}
		return message;
	}
	/**
	 * 保存模块信息
	 */
	@ResponseBody
	@RequestMapping(value="/saveModule.html")
	public Message saveModule(HttpServletRequest request) {
		ModuleInfo module = ObjectUtil.mapToEntity(ModuleInfo.class, this.getParameterMap(request));
		Message message = new Message();
		if (StringUtil.isNotEmpty(module.getId())) {
			ModuleInfo m = service.getModuleByCode(module.getCode());
			if (m != null && !m.getId().equals(module.getId())) {
				message.setErrorMessage("编号“" + module.getCode() + "”已存在！");
				return message;
			}
			service.update(module, "t_module");
		} else {
			if (service.getModuleByCode(module.getCode()) != null) {
				message.setErrorMessage("编号“" + module.getCode() + "”已存在！");
				return message;
			}
			service.save(module, "t_module");
		}
		message.setOkMessage("保存模块信息成功！");
		return message;
	}
	/**
	 * 删除字段
	 */
	@ResponseBody
	@RequestMapping(value="/deleteField.html")
	public Message deleteField(HttpServletRequest request, String id) {
		Message message = new Message();
		int result = 0;
		if (StringUtil.isNotEmpty(id)) {
			result = service.executeSql("delete from t_module_field where id = ?", id);
		}
		if (result > 0) {
			message.setOkMessage("删除成功！");
		} else {
			message.setErrorMessage("删除失败！");
		}
		return message;
	}
	/**
	 * 删除字段
	 */
	@ResponseBody
	@RequestMapping(value="/deleteModule.html")
	public Message deleteModule(HttpServletRequest request, String id) {
		Message message = new Message();
		int result = 0;
		if (StringUtil.isNotEmpty(id)) {
			result = service.executeSql("delete from t_module where id = ?", id);
		}
		if (result > 0) {
			message.setOkMessage("删除成功！");
		} else {
			message.setErrorMessage("删除失败！");
		}
		return message;
	}
	/**
	 * 删除查询字段
	 */
	@ResponseBody
	@RequestMapping(value="/deleteQuery.html")
	public Message deleteQuery(HttpServletRequest request, String id) {
		Message message = new Message();
		int result = 0;
		if (StringUtil.isNotEmpty(id)) {
			result = service.executeSql("delete from t_module_query where id = ?", id);
		}
		if (result > 0) {
			message.setOkMessage("删除成功！");
		} else {
			message.setErrorMessage("删除失败！");
		}
		return message;
	}
	/**
	 * 判断模块编码是否存在
	 */
	@ResponseBody
	@RequestMapping(value="/codeInModule.html")
	public Message codeInModule(HttpServletRequest request) {
		ModuleInfo module = ObjectUtil.mapToEntity(ModuleInfo.class, this.getParameterMap(request));
		Message message = new Message();
		String code = request.getParameter("code");
		module = service.getModuleByCode(code);
		if (module != null) {
			message.setOkMessage("true");
		} else {
			message.setOkMessage("false");
		}
		return message;
	}
}